<?php

/**
 * LEAGUEMan
 *
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class LM
{
    /** @var object Savant2 Template Engine */
    var $Savant2;
    /** @var object LMSession */
    var $LMSession;

    function singleton()
    {
        static $LM;

        if (!isset($LM)) {
            $LM = new LM;
        }

        return $LM;
    }

    function start()
    {
        $LM = LM::singleton();
        $LM->LMSession = LMSession::getInstance();
    }

    function stop() {}

    public static function setProject($p)
    {
        self::setSessionValue("_project_", $p);
    }

    public static function getProject()
    {
        return self::getSessionValue("_project_", 0);
    }

    public static function setTeam($t)
    {
        self::setSessionValue("_team_", $t);
    }

    public static function getTeam()
    {
        return self::getSessionValue("_team_", 0);
    }

    public static function setMatch($m)
    {
        self::setSessionValue("_match_", $m);
    }

    public static function getMatch()
    {
        return self::getSessionValue("_match_", 0);
    }

    public static function getSessionValueFromRequest($sessionkey, $requestkey, $default = null)
    {
        $session = LM::getObject('LMSession');

        if (!$session->has($sessionkey)) {
            $session->set($sessionkey, $default);
        }

        if (isset($_REQUEST[$requestkey])) {
            $session->set($sessionkey, $_REQUEST[$requestkey]);
        }

        return $session->get($sessionkey);
    }

    public static function unsetSessionValue($key)
    {
        $session = LM::getObject("LMSession");
        $session->delete($key);
    }

    public static function setSessionValue($key, $value)
                {
        $session = LM::getObject("LMSession");
        $session->set($key,$value);
    }

    public static function getSessionValue($key, $default = null)
                {
        $session = LM::getObject("LMSession");
        return $session->get($key, $default);
    }

    function getObject($objname)
    {
        $LM = LM::singleton();

        if (isset($LM->$objname) && is_object($LM->$objname)) {
            return $LM->$objname;
        } else {
            $obj = null;
            return $obj;
        }
    }

    function getTemplate()
    {
        if (!class_exists("Savant2", false)) {
            require_once 'Savant2.php';
        }

        $savant2 = new Savant2();
        $savant2->addPath("template", $GLOBALS["mosConfig_absolute_path"] . "/components/com_leagueman/templates");
        $savant2->addPath("resource", $GLOBALS["mosConfig_absolute_path"] . "/components/com_leagueman/resources");
        $savant2->loadFilter("trimwhitespace");

        return $savant2;
    }

    function _include_($name)
    {
        global $mainframe;

        return $mainframe->getCfg("absolute_path") . "/administrator/components/com_leagueman/admin.leagueman.{$name}.php";
    }

    function getLMTable($name)
    {
        global $database;

        $class = "LM_Table_$name";

        if ($database instanceof database) {
            return new $class($database);
        } else {
            return null;
        }
    }

    function getPageNav($total, $limitstart, $limit, $admin = false) {
        global $mosConfig_absolute_path;

        if($admin)
            $path = $mosConfig_absolute_path . "/administrator/includes/pageNavigation.php";
        else
            $path = $mosConfig_absolute_path . "/includes/pageNavigation.php";

        require_once $path;
        return new mosPageNav( $total, $limitstart, $limit  );
    }

    function redirect($act = "", $msg = "")
    {
        if (!empty($act)) {
            $link = "index2.php?option=com_leagueman&act=$act";
        } else {
            $link = "index2.php?option=com_leagueman";
        }
        
        mosRedirect($link, $msg);
        exit;
    }
}